package com.ecoolex.ying.space.job.scheduler;

import java.util.Calendar;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ecoolex.framework.scheduler.Job;
import com.ecoolex.framework.scheduler.JobComponent;
import com.ecoolex.ying.space.core.manager.EstateProjectActivityCouponOrderManager;
import com.ecoolex.ying.space.entity.EstateProjectActivityCouponOrder;

@JobComponent
public class EstateProjectActivityCouponOrderCheckJob {

	private static final Logger log = LoggerFactory.getLogger(EstateProjectActivityCouponOrderCheckJob.class);
	
	@Autowired
	private EstateProjectActivityCouponOrderManager orderManager;
	
	@Job(name = "ying.space.EstateProjectActivityCouponOrderCheckJob.autoCloseOrder",describe = "自动取消未支付的地产活动代金券订单")
	public void autoCloseOrder() {
		//订单超过30分钟
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MINUTE, -30);
		List<EstateProjectActivityCouponOrder> orderList = orderManager.getUnpaidAndExpiration(calendar.getTime(), 10);
		//自动取消
		orderList.forEach(order -> {
			try {
				orderManager.autoCancel(order, EstateProjectActivityCouponOrderCheckJob.class.getSimpleName());
			}catch (Exception e) {
				log.error("自动取消地产项目活动代金券订单异常，orderId={}",order.getOrderId(),e);
			}
		});
	}
	
}
